
//
//  ibPageView.m
//  insuranceAssistant
//
//  Created by caiqilin on 16/7/22.
//  Copyright © 2016年 zgcrichard. All rights reserved.
//
#import "ibPageView.h"
#import "UIImageView+WebCache.h"
@interface ibPageView() <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ibPageView
#pragma mark - 初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
        
    }
    return self;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.autoresizingMask = UIViewAutoresizingNone;
    [self setup];
}


- (void)setup
{
    [self startTimer];
}

+ (instancetype)pageView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    
    CGFloat scrollW = self.scrollView.frame.size.width;
    CGFloat scrollH = self.scrollView.frame.size.height;
    
    self.pageControl.center = self.scrollView.center;
    self.pageControl.ex_y = scrollH - 30;
    
    self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0);
    
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH);
    }
}

#pragma mark - setter方法的重写
- (void)setImageNames:(NSArray *)imageNames
{
    _imageNames = imageNames;
    
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i = 0; i<imageNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *banner = imageNames[i];
        imageView.tag = i;
        [imageView sd_setImageWithURL:[NSURL URLWithString:banner]];
//        [imageView setImageWithURL:[NSURL URLWithString:banner] placeholderImage:nil];
        imageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage:)];
        [imageView addGestureRecognizer:singleTap];
        [self.scrollView addSubview:imageView];
    }
    [self setNeedsLayout];
    
    self.pageControl.numberOfPages = imageNames.count;
    self.pageControl.hidesForSinglePage = YES;
}

- (void)setCurrentColor:(UIColor *)currentColor
{
    _currentColor = currentColor;
    
    self.pageControl.currentPageIndicatorTintColor = currentColor;
}

- (void)setOtherColor:(UIColor *)otherColor
{
    _otherColor = otherColor;
    
    self.pageControl.pageIndicatorTintColor = otherColor;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

#pragma mark - 定时器控制
- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)nextPage
{
    NSInteger page = self.pageControl.currentPage + 1;
    if (page == self.pageControl.numberOfPages) {
        page = 0;
    }
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = page * self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:offset animated:YES];
}

- (void)onClickImage:(UITapGestureRecognizer *)tap {
//    UIImageView *imageView = (UIImageView *)tap.view;
//    ibBanner *banner = self.imageNames[imageView.tag];
//    if(!banner.linkUrl) return;
//    NSString *linkUrl = banner.linkUrl;
//    linkUrl = [NSString stringWithFormat:@"%@?userStatusToken=%@",banner.linkUrl,[NSUserDefaults getDefaultValuebyKey:UserToken]];
//    
//    if ([_delegate respondsToSelector:@selector(pushToWebViewController:url:)]) {
//        [_delegate pushToWebViewController:self url:linkUrl];
//    }

}
-(UINavigationController *)getNavController {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *rootVC = window.rootViewController;
    if([rootVC isKindOfClass:[UINavigationController class]]){
        return (UINavigationController *)rootVC;
    }{
        return nil;
    }
}

@end
